import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import "package:uikit/utils/text_util.dart";
import 'package:uikit_example/pages/demo/register/view_model/register_view_model.dart';

import '../widgets/login_input.dart';
import 'login_base_state.dart';

class RegisterPage extends StatefulWidget {
  const RegisterPage({Key? key}) : super(key: key);

  @override
  _RegisterPageState createState() => _RegisterPageState();
}

class _RegisterPageState extends LoginBaseState<RegisterPage, RegisterViewModel> {
  String? userName;
  String? password;
  String? rePassword;
  String? imoocId;
  String? orderId;

  @override
  String get appBarTitle => "注册";

  @override
  String get bottomTitle => "注册";

  @override
  String get rightTitle => "登录";

  @override
  RegisterViewModel get viewModel => Provider.of<RegisterViewModel>(context, listen: false);

  @override
  List<Widget> buildContentChildren(BuildContext context) {
    return [
      LoginInput(
        '用户名',
        '请输入用户名',
        onChanged: (String text) {
          userName = text;
          checkInput();
        },
      ),
      LoginInput(
        '密码',
        '请输入密码',
        obscureText: true,
        onChanged: (String text) {
          password = text;
          checkInput();
        },
        focusChanged: (bool value) {
          viewModel.isProtected = value;
        },
      ),
      LoginInput(
        '确认密码',
        '请再次输入密码',
        obscureText: true,
        onChanged: (String text) {
          rePassword = text;
          checkInput();
        },
        focusChanged: (bool value) {
          viewModel.isProtected = value;
        },
      ),
      LoginInput(
        "慕课网ID",
        "请输入你的慕课网用户ID",
        keyboardType: TextInputType.number,
        onChanged: (String text) {
          imoocId = text;
          checkInput();
        },
      ),
      LoginInput(
        "课程订单号",
        "请输入课程订单号后四位",
        keyboardType: TextInputType.number,
        onChanged: (String text) {
          orderId = text;
          checkInput();
        },
      ),
      LoginInput(
        "课程订单号",
        "请输入课程订单号后四位",
        keyboardType: TextInputType.number,
        onChanged: (String text) {
          orderId = text;
          checkInput();
        },
      ),
      LoginInput(
        "课程订单号",
        "请输入课程订单号后四位",
        keyboardType: TextInputType.number,
        onChanged: (String text) {
          orderId = text;
          checkInput();
        },
      ),
      LoginInput(
        "课程订单号",
        "请输入课程订单号后四位",
        keyboardType: TextInputType.number,
        onChanged: (String text) {
          orderId = text;
          checkInput();
        },
      ),
      LoginInput(
        "课程订单号",
        "请输入课程订单号后四位",
        keyboardType: TextInputType.number,
        onChanged: (String text) {
          orderId = text;
          checkInput();
        },
      ),
      LoginInput(
        "课程订单号",
        "请输入课程订单号后四位",
        keyboardType: TextInputType.number,
        onChanged: (String text) {
          orderId = text;
          checkInput();
        },
      ),
      LoginInput(
        "课程订单号",
        "请输入课程订单号后四位",
        keyboardType: TextInputType.number,
        onChanged: (String text) {
          orderId = text;
          checkInput();
        },
      ),
      LoginInput(
        "课程订单号",
        "请输入课程订单号后四位",
        keyboardType: TextInputType.number,
        onChanged: (String text) {
          orderId = text;
          checkInput();
        },
      ),
    ];
  }

  /// 检查输入框输入
  void checkInput() {
    bool result1 = TextUtil.isEmpty(userName);
    bool result2 = TextUtil.isEmpty(password);
    bool result3 = TextUtil.isEmpty(rePassword);
    bool result4 = TextUtil.isEmpty(imoocId);
    bool result5 = TextUtil.isEmpty(orderId);
    if (result1 || result2 || result3 || result4 || result5) {
      viewModel.loginEnable = false;
    } else {
      viewModel.loginEnable = true;
    }
  }

  /// 检查参数
  @override
  void checkParams() {
    String? tips;
    if (password != rePassword) {
      tips = '两次密码不一致';
    } else if (orderId?.length != 4) {
      tips = "请输入订单号的后四位";
    }
    if (tips != null) {
      print(tips);
      return;
    }
    print("sendRegisterRequest begin");
    viewModel.sendUserRequest(userName!, password!, imoocId: imoocId, orderId: orderId);
    print("sendRegisterRequest end");
  }

  @override
  void pushNextPage() {
    Navigator.of(context).pushNamed("/Login");
  }
}
